package grsu.epam.airline.service;

import grsu.epam.airline.DAO.IEmployeeDAO;
import grsu.epam.airline.DAOImpl.DAOFactory;
import grsu.epam.airline.constant.Constant;
import grsu.epam.airline.model.Employee;
import grsu.epam.airline.model.Speciality;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class EmployeeService {
	public void addEmployee(int specialityId, String name, String surname) {
		Speciality speciality = new Speciality(specialityId);
		IEmployeeDAO employeeDao = (IEmployeeDAO) DAOFactory.getInstance()
				.getDAO(Constant.EmployeeDAO);
		Employee employee = new Employee(name, surname, speciality);
		try {
			employeeDao.addEmployee(employee);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DAOFactory.getInstance().closeConnection(
					employeeDao.getConnection());
		}
	}

	public ArrayList<Employee> getAllEmployee() {
		IEmployeeDAO employeeDao = (IEmployeeDAO) DAOFactory.getInstance()
				.getDAO(Constant.EmployeeDAO);
		ArrayList<Employee> employees = null;

		try {
			employees = (ArrayList<Employee>) employeeDao.getAllEmployee();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DAOFactory.getInstance().closeConnection(
					employeeDao.getConnection());
		}
		return employees;
	}

	public void deleteEmployee(int employeeId) {
		IEmployeeDAO employeeDao = (IEmployeeDAO) DAOFactory.getInstance()
				.getDAO(Constant.EmployeeDAO);
		try {
			employeeDao.deleteEmployee(employeeId);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DAOFactory.getInstance().closeConnection(
					employeeDao.getConnection());
		}
	}

	public Employee getEmployeeById(int employeeId) {
		IEmployeeDAO employeeDao = (IEmployeeDAO) DAOFactory.getInstance()
				.getDAO(Constant.EmployeeDAO);
		Employee employee = null;

		try {
			employee = (Employee) employeeDao.getEmployeeById(employeeId);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DAOFactory.getInstance().closeConnection(
					employeeDao.getConnection());
		}
		return employee;
	}

	public void updateEmployee(int employeeId, String name, String surname,
			int specialityId) {
		Speciality speciality = new Speciality(specialityId);
		IEmployeeDAO employeeDao = (IEmployeeDAO) DAOFactory.getInstance()
				.getDAO(Constant.EmployeeDAO);
		Employee employee = new Employee(employeeId, name, surname, speciality);
		try {
			employeeDao.updateEmployee(employee);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DAOFactory.getInstance().closeConnection(
					employeeDao.getConnection());
		}
	}

	public List<Employee> getEmployeesBySpeciality(int specialityId) {
		IEmployeeDAO employeeDao = (IEmployeeDAO) DAOFactory.getInstance()
				.getDAO(Constant.EmployeeDAO);
		ArrayList<Employee> employees = null;

		try {
			employees = (ArrayList<Employee>) employeeDao
					.getEmployeesBySpeciality(specialityId);
		} catch (SQLException e) {
			e.printStackTrace();
			} finally {
			DAOFactory.getInstance().closeConnection(
					employeeDao.getConnection());
		}
		return employees;
	}
}
